<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="接口编码">
          <el-input
            v-model="searchParam.interfaceCode"
            placeholder="请输入接口编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="接口名称">
          <el-input
            v-model="searchParam.interfaceName"
            placeholder="请输入接口名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="接口类型">
          <el-select
            v-model="searchParam.interfaceType"
            placeholder="请选择接口类型"
            clearable
          >
            <el-option
              v-for="item in interfaceTypeList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select
            v-model="searchParam.enabled"
            placeholder="请选择是否启用"
            clearable
          >
            <el-option
              v-for="item in enabledList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="接口编码"
          prop="interfaceCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="接口名称"
          prop="interfaceName"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="接口类型"
          prop="interfaceTypeLabel"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="请求方式"
          prop="httpMethod"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="鉴权方式"
          prop="authModeLabel"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="接口地址"
          prop="interfaceUrl"
          show-overflow-tooltip
          min-width="150"
        />
        <el-table-column label="是否启用" align="center" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.enabled === 1" type="success">启用</el-tag>
            <el-tag v-else type="info">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="handleDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <deexchangeinterfaceEdit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "SYSTEMDEEXCHANGEINTERFACE",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/dataExchange/exchangeInterface/queryByPage";

const deleteParam = {
  api: "/dataExchange/exchangeInterface/deleteList",
  data: {},
};

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  interfaceCode: "",
  interfaceName: "",
  interfaceType: "",
  enabled: "",
};

/** 删除交互接口 */
async function deleteInfo(data: object) {
  await request({
    url: `/dataExchange/exchangeInterface/delete`,
    method: "post",
    data: data,
  });
}

function handleDelete(id: string) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    const data = {
      id: id,
    };
    deleteInfo(data)
      .then(() => {
        ElMessage.success("删除成功");
        reset();
      })
      .catch(() => ElMessage.info("已取消删除"));
  });
}

//是否启用
const enabledList: any = ref([]);
async function getEnabledList() {
  const { data } = await request({
    url: `/system/dictionary/getSelectedItems/Enabled`,
    method: "post",
  });
  enabledList.value = data;
}
//接口类型
const interfaceTypeList: any = ref([]);
async function getInterfaceTypeList() {
  const { data } = await request({
    url: `/system/dictionary/getSelectedItems/DeInterfaceType`,
    method: "post",
  });
  interfaceTypeList.value = data;
}

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getInterfaceTypeList();
  getEnabledList();
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

// 查询详情
async function get(id: string) {
  const { data } = await request({
    url: `/dataExchange/exchangeInterface/get/${id}`,
    method: "post",
  });
  addDialog.data = data;
}

/** 打开交互服务数据接口表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改交互服务数据接口";
    get(roleObj.id);
  } else {
    addDialog.title = "新增交互服务数据接口";
  }
  addDialog.visible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>
